package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.UserService;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController {

    @DubboReference
    private UserService userService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    //跳转到注册页面
    @RequestMapping("register")
    public String register(){
        return "register";
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(TbUser tbUser,String code){
//        redisTemplate.delete(tbUser.getPhone());
        ValueOperations value = redisTemplate.opsForValue();
        String code2 = (String) value.get(tbUser.getPhone());
        if (StringUtils.isBlank(code2)){//StringUtils.isBlank(),lang3架包，这里判断是否为空 ,也就是redis中不存在
            return Result.response(ResultEnum.HASCODE);
        }else if (!code2.equals(code)){
            return Result.response(ResultEnum.CODE);
        }else if (code2.equals(code)){
            userService.add(tbUser);
        }
        return Result.ok();
    }

    @RequestMapping("sendMessages")
    @ResponseBody
    public Result sendMessages(String phone){
        rocketMQTemplate.convertAndSend("dongdongshop-sendMessages",phone);
        return Result.ok();
    }
}
